#!/bin/sh

DO_SH=0
DO_CSH=0
DO_KEYVAL=0

for arg in "$@"; do
	case "$arg" in
		--sh-syntax) DO_SH=1 ;;
		--csh-syntax) DO_CSH=1 ;;
		--kde-mac) DO_KEYVAL=1 ;;
	esac
done

unset DBUS_SESSION_BUS_PID
SESSION_FILE="/tmp/kdemac-dbus-session-`/usr/bin/id -u`"

if [ -f "${SESSION_FILE}" ]; then
	. "${SESSION_FILE}"
	if [ `/bin/ps -p "${DBUS_SESSION_BUS_PID}" | /usr/bin/grep -c "^ *${DBUS_SESSION_BUS_PID} "` -eq 0 ]; then
		/opt/kde4-deps/bin/dbus-launch --sh-syntax >"${SESSION_FILE}"
	fi
else
	/opt/kde4-deps/bin/dbus-launch --sh-syntax >"${SESSION_FILE}"
fi

. "${SESSION_FILE}"

if [ "$DO_SH" -eq 1 ]; then
	echo "DBUS_SESSION_BUS_ADDRESS='${DBUS_SESSION_BUS_ADDRESS}';"
	echo "export DBUS_SESSION_BUS_ADDRESS;"
	echo "DBUS_SESSION_BUS_PID=${DBUS_SESSION_BUS_PID};"
fi
if [ "$DO_CSH" -eq 1 ]; then
	echo "setenv DBUS_SESSION_BUS_ADDRESS '${DBUS_SESSION_BUS_ADDRESS}';"
	echo "set DBUS_SESSION_BUS_PID=${DBUS_SESSION_BUS_PID};"
fi
if [ "$DO_KEYVAL" -eq 1 ]; then
	echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"
	echo "DBUS_SESSION_BUS_PID=${DBUS_SESSION_BUS_PID}"
fi

exit 0
